/**
 * 下拉勾选树控件
 */
Ext.ux.ComboTree = function() {
	this.treeId = Ext.id() + '-tree';
	this.maxHeight = arguments[0].maxHeight || arguments[0].height
			|| this.maxHeight;
	this.tpl = new Ext.Template('<tpl for="."><div style="height:'
			+ this.maxHeight + 'px"><div id="' + this.treeId
			+ '"></div></div></tpl>');
	this.store = new Ext.data.SimpleStore({
		fields : [],
		data : [[]]
	});
	this.selectedClass = '';
	this.mode = 'local';
	this.triggerAction = 'all';
	this.onSelect = Ext.emptyFn;
	this.editable = false;
	
	// all:所有节点可选
	// exceptRoot:除根节点
	// folder：只有目录可选（非根\叶子）
	// leaf:只有叶子可选
//	this.selectValueModel = 'leaf';
//	this.selectModel = arguments[0].selectModel || 'sigle';
	
	Ext.ux.ComboTree.superclass.constructor.apply(this, arguments);
}

Ext.extend(Ext.ux.ComboTree, Ext.form.ComboBox, {

	initEvents : function() {
		Ext.ux.ComboTree.superclass.initEvents.apply(this, arguments);
		this.keyNav.tab = false;

	},

	initComponent : function() {
		this.on({
			scope : this
		});

	},
	expand : function() {
		Ext.ux.ComboTree.superclass.expand.call(this);		
		if (!this.tree.rendered) {
			this.tree.height = this.maxHeight;
			this.tree.border = false;
			this.tree.autoScroll = true;
			if (this.tree.xtype) {
				this.tree = Ext.ComponentMgr.create(this.tree, this.tree.xtype);
			}
			this.tree.render(this.treeId);
			var root = this.tree.getRootNode();
			if (!root.isLoaded())
				root.reload();
			
			this.tree.on('click', this.setValue,this);
		}
	},
	
	setValue : function(node) {
		if (this.selectValueModel == 'all'
				|| (this.selectValueModel == 'leaf' && node.isLeaf())
				|| (this.selectValueModel == 'folder' && !node.isLeaf())) {
			this.lastSelectionText = node.text;
			if (this.hiddenField) {
				this.hiddenField.value = node.id;
			}
			Ext.form.ComboBox.superclass.setValue.call(this,node.text?node.text:node);
			this.value = node.id;
			this.collapse();
		}
	},
	
	setComboValue : function(id,text){
		if(this.hiddenField){
			this.hiddenField.value = id;
		}
		Ext.form.ComboBox.superclass.setValue.call(this,text);
		this.value = id;
	},
	
	getValue : function() {
		return this.value || '';
	},
	
	getNode : function(){
		var node = this.tree.getSelectionModel().getSelectedNode();
		return node;
	},
	
	clearValue : function() {
		Ext.ux.ComboBoxTree.superclass.clearValue.call(this);
		this.node = null;
	}
});

Ext.reg('comboTree', Ext.ux.ComboTree);